package com.iwop.moto.controller;

import com.iwop.moto.comm.ErrCode;
import com.iwop.moto.model.ResultModel;
import com.iwop.moto.model.SuperRecordModel;
import com.iwop.moto.service.MGoodsService;
import com.iwop.moto.utils.stringUtil.SequenceUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * M_GOODS 商品管理
 * @author  Created by liuwb on 2016-07-25.
 */
@Controller
public class MGoodsController extends BaseController {

    @Autowired
    MGoodsService mgoodsService;

    /**
     *****************************
     * 功能:商品 - 新增
     * liuwb 2016-07-26 09:43
     * 参数:store_id 店铺ID
     * 参数:user_id 用户ID
     * 参数:goodname 商品名
     * 参数:type 类型
     * 参数:brand 品牌
     * 参数:guaranteetime 保修时间
     * 参数:returngoods 退货换货
     * 参数:imgpath 缩略图
     * 参数:startnums 多少起批
     * 参数:expressCharge 快递费用
     *****************************
     * 参数:mGoodsDetail json 格式数据
     * 例子:mGoodsDetail=[{
     *   "m_goods_id": "1",
     *   "model": "S",
     *   "colors": "#00FFFF",
     *   "stocknums": "10",
     *   "price1": "100"
     *   },{
     *   "m_goods_id": "1",
     *   "model": "M",
     *   "colors": "#CCFFFF",
     *   "stocknums": "10",
     *   "price1": "100"
     *   }]
     *****************************
     */
    @RequestMapping("/api/mgoods/add")
    @ResponseBody
    public ResultModel mgoodsAdd(){
        SuperRecordModel SRModel = this.getSRModel();
        try {
            String m_goods_id = UUID.randomUUID().toString();
            SRModel.put("sid", m_goods_id);
            SRModel.put("addtime",new Date());
            SRModel.put("goodsno", SequenceUtil.SerialID("G"));
            SRModel.put("checkstatus","0");
            SRModel.put("delete_flag","1");
            mgoodsService.insertSelective(SRModel);
            String mGoodsDetail = SRModel.getString("mGoodsDetail");
            if(!StringUtils.isEmpty(mGoodsDetail)){
                List<SuperRecordModel> lsDetail = SRModel.jsonToList(mGoodsDetail);
                for(SuperRecordModel srmDetail : lsDetail){
                    srmDetail.put("sid",UUID.randomUUID().toString());
                    srmDetail.put("m_goods_id",m_goods_id);
                    srmDetail.put("delete_flag","1");
                    srmDetail.put("ststus_time",new Date());
                    srmDetail.put("sellnums",0);
                    mgoodsService.insertDetailSelective(srmDetail);
                }
            }
            return getSucceed(ErrCode.SUCCEED);
        }catch (Exception e){
            e.printStackTrace();
            return getErr(ErrCode.ERROR_SYSTEM);
        }
    }

    /**
     * 功能:根据状态获取 销售中/审核中/审批未通过的商品列表
     * 参数:storeId 店铺id
     * 参数:status 状态 1销售中 0审核中 2未通过
     */
    @RequestMapping("/api/mgoods/list")
    @ResponseBody
    public ResultModel mgoodsList(){
        SuperRecordModel SRModel = this.getSRModel();
        try {
            List<SuperRecordModel> srmList = mgoodsService.findStoreByIdAndStatus(SRModel);
            for(SuperRecordModel srm : srmList){
                List<SuperRecordModel> ls = mgoodsService.findFgoodsListDetail(srm);
                if(ls.size()>0){
                    srm.put("mGoodsDetail",ls);
                }
            }
            return getSucceed(srmList,ErrCode.SUCCEED);
        }catch (Exception e){
            e.printStackTrace();
            return getErr(ErrCode.ERROR_SYSTEM);
        }
    }

    /**
     * 功能:更新商品发货地，描述
     * 参数:sid 商品id
     * 参数:address 商品发货地
     * 参数:describe 商品描述
     */
    @RequestMapping("/api/mgoods/address/describe")
    @ResponseBody
    public ResultModel mgoodsAddressDescribe(){
        SuperRecordModel SRModel = this.getSRModel();
        try {

            mgoodsService.upMGoodsAddressDescribe(SRModel);
            return getSucceed(ErrCode.SUCCEED);
        }catch (Exception e){
            e.printStackTrace();
            return getErr(ErrCode.ERROR_SYSTEM);
        }
    }

    /**
     * 功能:商品 - 主子详细信息
     * liuwb 2016-07-26 15:53
     * 参数: sid 主键
     */
    @RequestMapping("/api/mgoods/detail")
    @ResponseBody
    public ResultModel mgoodsDetail(){
        SuperRecordModel SRModel = this.getSRModel();
        try {
            SRModel =  mgoodsService.findFgoodsById(SRModel);
            SRModel.put("mGoodsDetail",mgoodsService.findFgoodsListDetail(SRModel));
            return getSucceed(SRModel,ErrCode.SUCCEED);
        }catch (Exception e){
            e.printStackTrace();
            return getErr(ErrCode.ERROR_SYSTEM);
        }
    }

    /**
     * 获取商品详情 - 规格
     */
    @RequestMapping("/api/mgoods/detail/bysid")
    @ResponseBody
    public ResultModel mgoodsDetailByid(){
        SuperRecordModel SRModel = this.getSRModel();
        try {
            List<SuperRecordModel> srm =  mgoodsService.findFgoodsDetailByGoodsId(SRModel);
            SRModel.put("mGoodsDetail",srm);
            if(srm!=null){
                SRModel.put("sid",SRModel.getString("sid"));
            }
            SuperRecordModel fgoodsById = mgoodsService.findFgoodsById(SRModel);
            if(fgoodsById!=null){
                SRModel.putAll(fgoodsById);
            }
            return getSucceed(SRModel,ErrCode.SUCCEED);
        }catch (Exception e){
            e.printStackTrace();
            return getErr(ErrCode.ERROR_SYSTEM);
        }
    }

    /**
     * 功能:商品 - 修改库存
     * liuwb 2016-07-26 15:11
     * 参数 ：jsonParamArr Json数据
     * json参数:sid 商品规格信息主键
     * json参数:stocknums 修改的库存数
     */
    @RequestMapping("/api/mgoods/stocknum")
    @ResponseBody
    public ResultModel upMGoodsStocknums(){
        SuperRecordModel SRModel = this.getSRModel();
        try {
            SRModel.put("status_time",new Date());
            mgoodsService.upMGoodsStocknums(SRModel);
            String jsonParamArr = SRModel.getString("jsonParamArr");
            if(!StringUtils.isEmpty(jsonParamArr)){
                List<SuperRecordModel> lists = SRModel.jsonToList(jsonParamArr);
                for(SuperRecordModel srm : lists){
                    srm.put("statusTime",new Date());
                    mgoodsService.upMGoodsDetailStocknums(srm);
                }
            }
            return getSucceed(ErrCode.SUCCEED);
        }catch (Exception e){
            e.printStackTrace();
            return getErr(ErrCode.ERROR_SYSTEM);
        }
    }


    /**
     * 功能: 商品修改审批状态
     * liuwb 2016-07-26 15:36
     * 参数: sid 商品主键
     * 参数: status [0:审核中][1:通过][2:拒绝][3:取消发布]
     * 参数: checkuserid 审批人id
     */
    @RequestMapping("/api/mgoods/checkstatus")
    @ResponseBody
    public ResultModel upMGoodsCheckstatus(){
        SuperRecordModel SRModel = this.getSRModel();
        try {
            String ckFlag = mgoodsService.getCheckStatusBySid(SRModel);
            if(SRModel.getString("status").equals("3") && !ckFlag.equals("0")){//只有审批中可以取消发布
                return getErr("只有审批中可以取消发布");
            }
            SRModel.put("checktime",new Date());
            mgoodsService.upMGoodsCheckstatus(SRModel);
            return getSucceed(ErrCode.SUCCEED);
        }catch (Exception e){
            e.printStackTrace();
            return getErr(ErrCode.ERROR_SYSTEM);
        }
    }

    /**
     * 功能: 商品修改上架状态
     * liuwb 2016-07-26 15:45
     * 参数: sid 商品主键
     * 参数: status [0:未发布][1:上架][2:下架]
     */
    @RequestMapping("/api/mgoods/statusflag")
    @ResponseBody
    public ResultModel upMGoodsStatusFlag(){
        SuperRecordModel SRModel = this.getSRModel();
        try {
            if(!StringUtils.isEmpty(SRModel.getString("sid"))){
                String sid1 = SRModel.getString("sid");
                String [] sids = sid1.split(",");
                for(String sid : sids){
                    SuperRecordModel srm = new SuperRecordModel();
                    srm.put("sid",sid);
                    srm.put("status",SRModel.getString("status"));
                    srm.put("statusTime",new Date());
                    mgoodsService.upMGoodsDetailStatusFlag(srm);
                }
            }
            if(!StringUtils.isEmpty(SRModel.getString("gid"))){
                String gid = SRModel.getString("gid");
                String [] sids = gid.split(",");
                for(String sid : sids){
                    SuperRecordModel srm = new SuperRecordModel();
                    srm.put("sid",sid);
                    srm.put("status",SRModel.getString("status"));
                    srm.put("statusTime",new Date());
                    mgoodsService.upMGoodsStatusFlag(srm);
                }
                SRModel.put("statusTime",new Date());
            }
            if(!StringUtils.isEmpty(SRModel.getString("m_goods_id"))){
                String mGoodsId = SRModel.getString("m_goods_id");
                String[] split = mGoodsId.split(",");
                for (String mGoods:split){
                    boolean isOut=mgoodsService.findFGoodsdetailIsOut(mGoods);
                    if (isOut){
                        SuperRecordModel srm=new SuperRecordModel();
                        srm.put("sid",mGoods);
                        srm.put("status","2");
                        srm.put("statusTime",new Date());
                        mgoodsService.upMGoodsStatusFlag(srm);
                    }
                }
            }
            return getSucceed(ErrCode.SUCCEED);
        } catch (Exception e) {
            e.printStackTrace();
            return getErr(ErrCode.ERROR_SYSTEM);
        }
    }

    /**
     * 删除商品
     * 参数：checkstatus 状态传2
     * 参数：sid 订单id
     * */
    @RequestMapping("/api/mgoods/delete")
    @ResponseBody
    public ResultModel upMgoodsDeleteFlag(){
        SuperRecordModel SRModel = this.getSRModel();
        String goodIds = SRModel.getString("goods_id");
        String detailS = SRModel.getString("detail_id");
        try {
            String[] goodsArr = goodIds.split(",");
            String[] detailArr = detailS.split(",");
            if(goodsArr.length>0){
                for (String goodId:goodsArr){
                    if(!StringUtils.isEmpty(goodId)){
                        SuperRecordModel srm=new SuperRecordModel();
                        srm.put("sid",goodId);
                        SuperRecordModel fgoodsById = mgoodsService.findFgoodsById(srm);
                        if(fgoodsById.getString("checkstatus").equals("2")||fgoodsById.getString("status_flag").equals("2")){
                            SRModel.put("deleteTime",new Date());
                            SRModel.put("deleteFlag","0");
                            SRModel.put("sid",goodId);
                            mgoodsService.upMgoodsDeleteFlag(SRModel);
                        }
                    }
                }
            }
            if (detailArr.length>0){
                for (String goodId:detailArr){
                    if(!StringUtils.isEmpty(goodId)){
                        SuperRecordModel srm=new SuperRecordModel();
                        srm.put("sid",goodId);
                        SuperRecordModel fgoodsById = mgoodsService.findFgoodsDetailById(srm);
                        if(fgoodsById.getString("status_flag").equals("2")){
                            SRModel.put("deleteTime",new Date());
                            SRModel.put("deleteFlag","0");
                            SRModel.put("sid",goodId);
                            mgoodsService.upMgoodsDetailDelFlag(SRModel);
                        }
                    }
                }
            }
            return getSucceed(ErrCode.SUCCEED);
        } catch (Exception e) {
            e.printStackTrace();
            return getErr(ErrCode.ERROR_SYSTEM);
        }
    }

    @RequestMapping("/api/mgoods/limit/list")
    @ResponseBody
    public ResultModel get(){

        return null;
    }
}
